package pro.softzhang.algo.lc800;

import pro.softzhang.algo.common.ListNode;
import pro.softzhang.algo.flag.book.BDaily;

import java.util.HashSet;
import java.util.Set;

@BDaily
public class LC817_LinkedListComponents {
    public static void main(String[] args) {

    }

    static class Solution {
        public int numComponents(ListNode head, int[] nums) {
            // 方便查找转成Set
            Set<Integer> s = new HashSet<>();
            for (int num : nums) {
                s.add(num);
            }

            int count = 0;
            while (head != null) {
                if (!s.contains(head.val)) {
                    head = head.next;
                } else {
                    while (head != null && s.contains(head.val)){
                        head = head.next;
                    }
                    count++;
                }
            }

            return count;
        }
    }
}
